package example.com.zcq.mymj.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import example.com.zcq.mymj.R;
import example.com.zcq.mymj.fragment.Fragment1;
import example.com.zcq.mymj.fragment.Fragment2;
import example.com.zcq.mymj.fragment.Fragment3;
import example.com.zcq.mymj.fragment.Fragment4;
import example.com.zcq.mymj.fragment.Fragment6;
import example.com.zcq.mymj.fragment.FragmentLuntang;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;

    //Fragment Object
    private Fragment1 fg1;
//    private Fragment2 fg6;
    private FragmentLuntang fg3;
    private Fragment4 fg4;
    private Fragment6 fg6;
    private FragmentManager fManager;
    /**
     * 首页
     */
    private RadioButton mRbChannel;
    /**
     * 学习
     */
    private RadioButton mRbMessage;
    /**
     * 资讯
     */
    private RadioButton mRbBetter;
    /**
     * 设置
     */
    private RadioButton mRbSetting;
    private RadioGroup mRgTabBar;
    private View mDivTabBar;
    private FrameLayout mLyContent;
    /**
     * 首页
     */
    private RadioButton mRb1;
    /**
     * 学习
     */
    private RadioButton mRb2;
    /**
     * 资讯
     */
    private RadioButton mRb3;
    /**
     * 设置
     */
    private RadioButton mRb4;
    private RadioButton mRb5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        fManager = getSupportFragmentManager();
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //获取第一个单选按钮，并设置其为选中状态
        rb_channel = (RadioButton) findViewById(R.id.rb_1);
        rb_channel.setChecked(true);


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId) {
            case R.id.rb_1:
                if (fg1 == null) {
                    fg1 = new Fragment1();
                    fTransaction.add(R.id.ly_content, fg1);
                } else {
                    fTransaction.show(fg1);
                }
                break;
            case R.id.rb_2:
                if (fg6 == null) {
                    fg6 = new Fragment6();
                    fTransaction.add(R.id.ly_content, fg6);
                } else {
                    fTransaction.show(fg6);
                }
                break;
            case R.id.rb_3:
                if (fg3 == null) {
                    fg3 = new FragmentLuntang();
                    fTransaction.add(R.id.ly_content, fg3);
                } else {
                    fTransaction.show(fg3);
                }
                break;
            case R.id.rb_4:
                if (fg4 == null) {
                    fg4 = new Fragment4();
                    fTransaction.add(R.id.ly_content, fg4);
                } else {
                    fTransaction.show(fg4);
                }
                break;
//            case R.id.rb_5:
//                if (fg6 == null) {
//                    fg6 = new Fragment6();
//                    fTransaction.add(R.id.ly_content, fg6);
//                } else {
//                    fTransaction.show(fg6);
//                }
//                break;
        }
        fTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fg1 != null)
            fragmentTransaction.hide(fg1);
        if (fg6 != null)
            fragmentTransaction.hide(fg6);
        if (fg3 != null)
            fragmentTransaction.hide(fg3);
        if (fg4 != null)
            fragmentTransaction.hide(fg4);

    }


    private void initView() {
        mRb1 = (RadioButton) findViewById(R.id.rb_1);
        mRb2 = (RadioButton) findViewById(R.id.rb_2);
        mRb3 = (RadioButton) findViewById(R.id.rb_3);
        mRb4 = (RadioButton) findViewById(R.id.rb_4);
        mRb5 = (RadioButton) findViewById(R.id.rb_5);
        mRgTabBar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        mDivTabBar = (View) findViewById(R.id.div_tab_bar);
        mLyContent = (FrameLayout) findViewById(R.id.ly_content);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        if(name!=null){
            Bundle bundle=new Bundle();
            bundle.putString("name",name);
            fg4.setArguments(bundle);

        }
    }

}
